package com.cskaoyan.app.service.impl;

import com.cskaoyan.app.bean.common.BasePageInfo;
import com.cskaoyan.app.bean.common.CommonData;
import com.cskaoyan.app.bean.po.MarketAd;
import com.cskaoyan.app.bean.po.MarketAdExample;
import com.cskaoyan.app.mapper.MarketAdMapper;
import com.cskaoyan.app.service.AdminAdService;
import com.cskaoyan.app.util.StringsUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdminAdServiceImpl implements AdminAdService {

    @Autowired
    private MarketAdMapper adMapper;

    @Override
    public CommonData<MarketAd> list(BasePageInfo info, String name, String content) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        MarketAdExample example = new MarketAdExample();

        example.setOrderByClause(StringsUtil.getOrderClause(info.getSort(), info.getOrder()));
        MarketAdExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);

        if (!StringsUtil.isEmpty(name)) {
            // criteria.andNameLike("%" + name + "%");
            criteria.andNameLike(StringsUtil.getConcat(name));
        }

        if (!StringsUtil.isEmpty(content)) {
            // criteria.andContentLike("%"+content+"%");
            criteria.andContentLike(StringsUtil.getConcat(content));
        }

        List<MarketAd> list = adMapper.selectByExample(example);

        PageInfo<MarketAd> pageInfo = new PageInfo<>(list);

        return CommonData.data(pageInfo);
    }

    @Override
    public void update(MarketAd marketAd) {
        marketAd.setUpdateTime(new Date());
        adMapper.updateByPrimaryKeySelective(marketAd);
    }

    @Override
    public void delete(MarketAd marketAd) {
        marketAd.setDeleted(true);
        adMapper.updateByPrimaryKeySelective(marketAd);
    }

    @Override
    public MarketAd create(MarketAd marketAd) {
        marketAd.setAddTime(new Date());
        marketAd.setUpdateTime(new Date());
        marketAd.setEndTime(new Date());
        marketAd.setStartTime(new Date());
        adMapper.insertSelective(marketAd);
        return adMapper.selectByPrimaryKey(marketAd.getId());
    }
}
